﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ComputerStoreManager;
using Modules.Core;
using Modules.Library.Entities;

namespace ComputerStoreManager.ViewModel {

    public class ViewCustomerControlViewModel : BasePersonControlViewModel {

        #region ICommand
        public ICommand PreviewCommand {
            get;
            private set;
        }
        public ICommand NextCommand {
            get;
            private set;
        }
        public ICommand ChangeCustomerInfoCommand {
            get;
            private set;
        }
        public ICommand SearchCommand {
            get;
            private set;
        }
        public ICommand ShowAllCommand {
            get;
            private set;
        }
        public ICommand SelectionChangedCommand {
            get;
            private set;
        }
        #endregion

        #region Variables
        private TextBox txtBonusPoint;
        private TextBox txtDateOfRegister;
        private TextBox txtSearch;
        private ComboBox cbbSearchBy;
        private ComboBox cbbSex;
        private Button btnChangeCustomerInfo;
        private Button btnNext;
        private Button btnPreview;
        private Button btnSearch;
        private Button btnShowAll;
        private DataGrid dataGrid1;
        private DataTable dataTemp;

        private int counter;

        #endregion

        public ViewCustomerControlViewModel(TextBox txtAddress, TextBox txtBonusPoint, TextBox txtCustomerId, TextBox txtCustomerName, TextBox txtDateOfBirth,
            TextBox txtDateOfRegister, TextBox txtEmail, TextBox txtIdentifier, TextBox txtPhone, TextBox txtSearch, ComboBox cbbSearchBy,
            ComboBox cbbSex, Button btnChangeCustomerInfo, Button btnNext, Button btnPreview, Button btnSearch, Button btnShowAll, DataGrid dataGrid1)
            : base(txtCustomerName, txtCustomerId, txtDateOfBirth, txtIdentifier, txtPhone, txtAddress, txtEmail, null, null) {
            // TODO: Complete member initialization
            this.txtBonusPoint = txtBonusPoint;
            this.txtDateOfRegister = txtDateOfRegister;
            this.txtSearch = txtSearch;
            this.cbbSearchBy = cbbSearchBy;
            this.cbbSex = cbbSex;
            this.btnChangeCustomerInfo = btnChangeCustomerInfo;
            this.btnNext = btnNext;
            this.btnPreview = btnPreview;
            this.btnSearch = btnSearch;
            this.btnShowAll = btnShowAll;
            this.dataGrid1 = dataGrid1;

            LoadData();
            InitCmd();
        }
        public void ReloadData() {
            LoadData();
        }

        private void InitCmd() {
            try
            {

                PreviewCommand = new ExecutableCommand(DoPreview);
                NextCommand = new ExecutableCommand(DoNext);
                ChangeCustomerInfoCommand = new ExecutableCommand(DoChangeCustomerInfo);
                SearchCommand = new ExecutableCommand(DoSearch);
                ShowAllCommand = new ExecutableCommand(DoShowAll);
                SelectionChangedCommand = new ExecutableCommand(SelectionChangedEvent);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void SelectionChangedEvent(object obj) {
            try {
                if (dataTemp != null && dataTemp.Rows.Count > 0) {
                    LoadPersonInfoByCounter(counter = dataGrid1.SelectedIndex);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoChangeCustomerInfo() {
            try {
                if (CheckIsNotNull()) {
                    Customer customer = new Customer();
                    customer.DiemThuong = Convert.ToInt32(txtBonusPoint.Text);
                    customer.Email = base.TxtEmail.Text;
                    customer.DiaChi = base.TxtAddress.Text;
                    customer.HoVaTen = base.TxtPersonFullName.Text;
                    customer.MaKhachHang = base.TxtPersonId.Text;
                    customer.SoCMND = base.TxtIdentifier.Text;
                    customer.SoDienThoai = base.TxtPhone.Text;

                    customer.GioiTinh = ((ComboBoxItem)cbbSex.SelectedValue).Content.ToString();
                    customer.NgayDangKy = DateTime.Parse(Converter.ConvertToDateTime(txtDateOfRegister.Text, Converter.MMDDYYYY));
                    customer.NgaySinh = DateTime.Parse(Converter.ConvertToDateTime(txtDateOfRegister.Text, Converter.MMDDYYYY));

                    if (!Model.UpdateData(customer, KindOfObject.BY_CUSTOMER)) {
                        MessageBox.Show(Model.Error);
                    }
                    else {
                        ReloadData();
                        MessageBox.Show("Thành công");
                    }

                }
                else {
                    MessageBox.Show("Bạn không thể bỏ trống bất cứ trường nào", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }

        }

        private void DoSearch() {
            try {
                var data = Model.SearchInfo(txtSearch.Text, (SearchingCondition)cbbSearchBy.SelectedIndex, KindOfObject.BY_CUSTOMER);
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }


        private void DoShowAll() {
            try {
                var data = Model.GetListKhachHang();
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoPreview() {
            if (counter > 0) {
                counter--;
            }
            LoadPersonInfoByCounter(counter);
        }

        private void DoNext() {
            if (counter < dataTemp.Rows.Count - 1) {
                counter++;
            }
            LoadPersonInfoByCounter(counter);
        }

        private void LoadData() {
            try {
                dataTemp = Model.GetListKhachHang();
                counter = 0;
                if (dataTemp != null && dataTemp.Rows.Count > 0) {
                    if (dataGrid1.SelectedIndex >= 0) {
                        LoadPersonInfoByCounter(counter);
                    }
                    else {
                        LoadPersonInfoByCounter(0);
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void LoadPersonInfoByCounter(int counter) {
            try {

                base.TxtAddress.Text = dataTemp.Rows[counter]["DiaChi"].ToString();
                base.TxtEmail.Text = dataTemp.Rows[counter]["Email"].ToString();
                base.TxtIdentifier.Text = dataTemp.Rows[counter]["CMND"].ToString();
                base.TxtPhone.Text = dataTemp.Rows[counter]["SoDienThoai"].ToString();
                base.TxtPersonId.Text = dataTemp.Rows[counter]["MaKH"].ToString();
                base.TxtPersonFullName.Text = dataTemp.Rows[counter]["HoVaTen"].ToString();
                base.TxtDateOfBirth.Text = DateTime.Parse(dataTemp.Rows[counter]["NgaySinh"].ToString()).ToString("dd/MM/yyyy");
                cbbSex.SelectedIndex = dataTemp.Rows[counter]["GioiTinh"].ToString().Equals("Nam") ? 0 : 1;
                txtBonusPoint.Text = dataTemp.Rows[counter]["DiemThuong"].ToString();
                txtDateOfRegister.Text = DateTime.Parse(dataTemp.Rows[counter]["NgayDangKy"].ToString()).ToString("dd/MM/yyyy");
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác " + ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public override bool CheckIsNotNull() {
            return
                //TxtAddress.Text != " " &&
            TxtPersonId.Text != " " &&
            TxtPersonFullName.Text != " " &&
                //TxtUserName.Text != " " &&
            TxtDateOfBirth.Text != " " &&
            TxtPhone.Text != " " &&
            TxtEmail.Text != " " &&
            TxtIdentifier.Text != " ";
        }

    }
}
